package com.ewhl.mark.meijialian;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.ewhl.mark.meijialian.volley.BaseVO;
import com.ewhl.mark.meijialian.volley.MyErrorListener;
import com.ewhl.mark.meijialian.volley.MyReponseListener;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.HashMap;
import java.util.logging.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;


public class LoginActivity extends AppCompatActivity  implements PlatformActionListener {

    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.tv_shop_namee)
    TextView tvShopNamee;
    @BindView(R.id.login_header)
    AutoLinearLayout loginHeader;

    @BindView(R.id.please_enter_your_mobile_phone_et)
    EditText pleaseEnterYourMobilePhoneEt;
    @BindView(R.id.please_input_a_password)
    EditText pleaseInputAPassword;
    @BindView(R.id.all_denglu)
    AutoLinearLayout allDenglu;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.register_tv)
    TextView registerTv;
    @BindView(R.id.forget_password_tv)
    TextView forgetPasswordTv;
    @BindView(R.id.login_byweixin)
    AutoLinearLayout loginByweixin;
    @BindView(R.id.login_byqq)
    AutoLinearLayout loginByqq;
    NetValues netValues;
    String url="http://chengjing.cnewhl.com/index.php/Mobile/Index/loadding";
    int tag=0;
    Dialog dialog;
    Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        netValues=NetValues.getInstance(this);
        mContext=this;
//        if(getIntent().getStringExtra("load_url")!=null){
//            url=getIntent().getStringExtra("load_url");
//            tag=1;
//        }

    }


    @OnClick({R.id.white_return_iv, R.id.btn_login, R.id.register_tv, R.id.forget_password_tv, R.id.login_byweixin, R.id.login_byqq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                CommonUtils.goToWeb(mContext,CommonWebActivity.class,url);
                finish();
                break;
            case R.id.btn_login:
                if(CommonUtils.checkNull(pleaseEnterYourMobilePhoneEt.getText().toString())){
                    if(CommonUtils.checkNull(pleaseInputAPassword.getText().toString())){
                        netValues.login(pleaseEnterYourMobilePhoneEt.getText().toString(), pleaseInputAPassword.getText().toString(), new MyReponseListener() {
                            @Override
                            public void onResponse(BaseVO arg0) {
                                LoginBean loginBean=(LoginBean)arg0;
                                if(loginBean.getStatus()==1){
                                  String key=  loginBean.getResult();
                                  SPUtils.put(LoginActivity.this,"key",key);
                                  CommonUtils.goToWeb(LoginActivity.this,CommonWebActivity.class,url+"/key/"+key);
                                  finish();
                                }else {
                                    Toast.makeText(LoginActivity.this, ""+loginBean.getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, new MyErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                super.onErrorResponse(error);
                            }
                        });
                    }
                }

                break;
            case R.id.register_tv:
                CommonUtils.goToWeb(this,CommonWebActivity.class,"http://chengjing.cnewhl.com/index.php/Mobile/User/reg");
                CommonUtils.addDestoryActivity(this,"LoginActivity");
                break;
            case R.id.forget_password_tv:
                CommonUtils.goToWeb(this,CommonWebActivity.class,"http://chengjing.cnewhl.com/index.php/Mobile/User/forget_pwd");
                break;
            case R.id.login_byweixin:
                dialog = DialogCreator.createLoadingDialog(LoginActivity.this,
                        "登录中");
                dialog.show();
                break;
            case R.id.login_byqq:
                dialog = DialogCreator.createLoadingDialog(LoginActivity.this,
                        "登录中");
                dialog.show();
                Platform qq = ShareSDK.getPlatform(QQ.NAME);

                qq.setPlatformActionListener(this);

                qq.showUser(null);
                break;
        }
    }

    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
        if (action == Platform.ACTION_USER_INFOR) {
            final PlatformDb platDB = platform.getDb();//获取数平台数据DB
            //通过DB获取各种数据
            Log.d("SRG",platDB.exportData());
            platDB.getToken();
            netValues.checkReg(platDB.getUserId(), "qq", new MyReponseListener() {
                @Override
                public void onResponse(BaseVO arg0) {
                    LoginBean checkLoginBean=(LoginBean) arg0;
                    if(checkLoginBean.status==200){
                        SPUtils.put(LoginActivity.this, "key", checkLoginBean.result);

                        CommonUtils.goToWeb(LoginActivity.this,CommonWebActivity.class,url+"/key/"+checkLoginBean.result);
                        finish();
                    }else {
                        dialog.dismiss();
                        CommonUtils.addDestoryActivity(LoginActivity.this,"LoginActivity");
                        Intent it=new Intent(LoginActivity.this, CompleteRegisterActivity.class);
                        it.putExtra("header", platDB.getUserIcon());
                        it.putExtra("nickname",platDB.getUserName());
                        it.putExtra("openid",platDB.getUserId());
                        if(platDB.getUserGender().equals("m")){
                            it.putExtra("sex",1);
                        }else if(platDB.getUserGender().equals("f")){
                            it.putExtra("sex",2);
                        }
                        it.putExtra("oauth","qq");
                        startActivity(it);
                    }
                }
            }, new MyErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    super.onErrorResponse(error);
                }
            });

        }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Toast.makeText(this, "CANCEL", Toast.LENGTH_SHORT).show();

    }
}
